﻿Imports System.Data
Public Class frmInicial

#Region "Variáveis"

    Private objConfigurar As New Controle.configurarInicial(Application.StartupPath)

    Private dsModulos As New DataSet
    Private iCodUsuario As Integer
    Private iCodGrupoAcesso As Integer

#End Region

#Region "Propriedades"

    Public Shared Property codSGBD As PersistenciaPadrao.AcessoBD.eSGBD

#End Region

#Region "Construtor"

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        PersistenciaPadrao.AcessoBD.sCaminhoAplicacao = Application.StartupPath
        PersistenciaPadrao.AcessoBD.codSGBD = PersistenciaPadrao.AcessoBD.eSGBD.SQL_SERVER

    End Sub

#End Region

#Region "Eventos"

    Private Sub frmInicial_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            ' If Me.verificarConexao Then

            'If Me.loginUsuario Then

            '    Me.configuraGrid()
            '    Me.objConfigurar.dsModulos.Clear()
            Me.objConfigurar.lerXML(My.Resources.modulos)

            '    Me.dsModulos = Nothing
            '    Me.dsModulos = objConfigurar.dsModulos
            '    Me.dgvModulos.DataSource = Me.dsModulos.Tables(0)

            'Else
            '    Me.Close()
            'End If

            'Else
            'Me.Close()
            'End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub frmInicial_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.objConfigurar.fecharModulos()
    End Sub

    Private Sub lblLogoff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblLogoff.Click

        Me.objConfigurar.fecharModulos()

        Me.txtUsu.Text = String.Empty

        frmInicial_Load(sender, e)

    End Sub

    Private Sub lblAlteraSenha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAlterarSenha.Click
        Dim frmAlterarSenha As New FronteiraPadrao.frmAlterarSenha
        frmAlterarSenha.ShowDialog()
    End Sub

    Private Sub dgvModulos_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

        If String.IsNullOrWhiteSpace(Me.dgvModulos.Item("CaminhoModulo", e.RowIndex).Value) Then
            Me.selecionarModulo()
        End If

    End Sub

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click

        Dim sCaminhoModulo As String = String.Empty

        sCaminhoModulo = Me.dgvModulos.CurrentRow.Cells("CaminhoModulo").Value
        If IO.File.Exists(sCaminhoModulo) Then

            Me.objConfigurar.executarModulos(sCaminhoModulo,
                                             Application.StartupPath,
                                             Me.iCodUsuario,
                                             Me.iCodGrupoAcesso)

            Me.objConfigurar.atualizarXML(dsModulos)

        Else
            Me.selecionarModulo()
        End If

    End Sub

    Private Sub lblSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblSair.Click
        Me.objConfigurar.fecharModulos()
        Me.Close()
    End Sub

#End Region

#Region "Métodos"

    Private Sub configuraGrid()

        Try

            With Me.dgvModulos

                .DataSource = Nothing

                Dim colCodigo As New DataGridViewTextBoxColumn
                With colCodigo
                    .HeaderText = ""
                    .Name = "CodigoModulo"
                    .DataPropertyName = "CodigoModulo"
                    .Width = 50
                    .ReadOnly = True
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.None
                    .Visible = False
                    .SortMode = DataGridViewColumnSortMode.NotSortable
                End With
                .Columns.Add(colCodigo)

                Dim colNome As New DataGridViewTextBoxColumn
                With colNome
                    .HeaderText = ""
                    .Name = "NomeModulo"
                    .DataPropertyName = "NomeModulo"
                    .Width = 150
                    .ReadOnly = True
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Visible = True
                    .SortMode = DataGridViewColumnSortMode.NotSortable
                End With
                .Columns.Add(colNome)

                Dim colCaminho As New DataGridViewTextBoxColumn
                With colCaminho
                    .HeaderText = ""
                    .Name = "CaminhoModulo"
                    .DataPropertyName = "CaminhoModulo"
                    .Width = 50
                    .ReadOnly = True
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.None
                    .Visible = False
                    .SortMode = DataGridViewColumnSortMode.NotSortable
                End With
                .Columns.Add(colCaminho)

                .Refresh()

            End With

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Public Function verificarConexao() As Boolean

        Dim objAcesso As New PersistenciaPadrao.AcessoBD

        Dim bConexaoRealizada As Boolean = False
        Dim bTesteConexao As Boolean = False

        bTesteConexao = objAcesso.testarConexao(PersistenciaPadrao.Uteis.lerArquivoConexao)

        If Not bTesteConexao Then
            Dim frmConexao As New FronteiraPadrao.frmConexaoBD
            frmConexao.ShowDialog()
            bConexaoRealizada = frmConexao.conexaoRealizada
        Else
            bConexaoRealizada = True
        End If

        Return bConexaoRealizada

    End Function

    Public Function loginUsuario() As Boolean

        Dim bLogin As Boolean = False
        Dim frmLogin As New frmLogin

        frmLogin.ShowDialog()

        If frmLogin.loginRealizado Then

            Me.iCodUsuario = frmLogin.codUsuario
            Me.iCodGrupoAcesso = frmLogin.codGrupoAcesso
            Me.txtUsu.Text = frmLogin.nomeUsuario

            bLogin = True
        Else
            bLogin = False
        End If

        Return bLogin

    End Function

    Public Sub selecionarModulo()

        Dim openDlg As OpenFileDialog = New OpenFileDialog()
        openDlg.Title = "Selecionar Executável"
        openDlg.Filter = "Executável (*.exe)|*.exe"

        If (openDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then

            Me.dgvModulos.CurrentRow.Cells("CaminhoModulo").Value = openDlg.FileName

        End If

    End Sub

#End Region

End Class
